<?php
    require_once('popup_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class popup_edit_bid extends popup_base_jectly {

        protected $contents_html='<div class=\'center\'>This is popup_edit_bid page.</div>';

        protected $id;//project_id
        public $project;
		public $project_bidByUserID;
        protected $bid_id;//bid_id
        public $bid;//this can be unique only by project id but will check from query string too
        
          //forms
        public $textbox_bid;
        public $textbox_delivery_within;
        public $textbox_comment;
        public $button_confirm;
        public $button_cancel;
        public $button_back;
        public $button_ok;
        //flags
        public $bool_confirming=false;
        public $bool_message_exist=false;
        public $bool_error=false;
        public $bool_permitted=false;
        //messages
        public $str_confirming_text='Are you sure to edit this bid?<br /><br />';
        public $str_message;
        public $str_permission_error_text='no permission';
        
        

        function __construct() {
            parent::__construct();
        }

        protected function Form_Create() {

            $this->check_query_string();

            $this->load_project();
             $this->load_bid();
            
			 if($this->bool_edit_bid_permitted())
            {
                $this->bool_permitted=true;
            }

            if($this->bool_permitted)
            {
                $this->textbox_bid_Create();
                $this->textbox_delivery_within_Create();
                $this->textbox_comment_Create();
                $this->button_confirm_Create();
                $this->button_cancel_Create();
                $this->button_back_Create();
                $this->button_ok_Create();
            }
		
             
		}
		
		  protected function textbox_bid_Create() {
            $this->textbox_bid = new QTextBox($this);
            $this->textbox_bid->MaxLength=20;
            $this->textbox_bid->Width=60;
            $this->textbox_bid->Name = 'bid(*)';
            $this->textbox_bid->CssClass = 'input';
            if($this->bool_confirming)$this->textbox_bid->Visible=false;
        }

        protected function textbox_delivery_within_Create() {
            $this->textbox_delivery_within = new QTextBox($this);
            $this->textbox_delivery_within->MaxLength=20;
            $this->textbox_delivery_within->Width=60;
            $this->textbox_delivery_within->Name = 'delivery_within(*)';
            $this->textbox_delivery_within->CssClass = 'input';
            if($this->bool_confirming)$this->textbox_delivery_within->Visible=false;
        }

        protected function textbox_comment_Create() {
            $this->textbox_comment = new QTextBox($this);
            $this->textbox_comment->Width=400;
            $this->textbox_comment->Columns=50;
            $this->textbox_comment->Rows=10;
            $this->textbox_comment->Name = 'comment';
            //$this->textbox_comment->CssClass = 'input';
            $this->textbox_comment->Visible=true;
            $this->textbox_comment->Wrap=true;
            $this->textbox_comment->TextMode=QTextMode::MultiLine;
            $this->textbox_comment->Overflow=QOverflow::Auto;
            if($this->bool_confirming)$this->textbox_comment->Visible=false;
        }

        protected function button_confirm_Create() {
            $this->button_confirm = new QButton($this);
            $this->button_confirm->Text = 'confirm';
            $this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
            $this->button_confirm->PrimaryButton = true;
        }

        protected function button_cancel_Create() {
            $this->button_cancel = new QButton($this);
            $this->button_cancel->Text = 'cancel';
            $this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
            //$this->button_cancel->PrimaryButton = true;
        }

        protected function button_back_Create() {
            $this->button_back = new QButton($this);
            $this->button_back->Text = 'back';
            $this->button_back->AddAction(new QClickEvent(), new QServerAction('back_Click'));
            //$this->button_back->PrimaryButton = true;
        }

        protected function button_ok_Create() {
            $this->button_ok = new QButton($this);
            $this->button_ok->Text = 'ok';
            $this->button_ok->AddAction(new QClickEvent(), new QServerAction('ok_Click'));
            //$this->button_ok->PrimaryButton = true;
        }

        public function confirm_Click($strFormId, $strControlId, $strParameter) {

            $this->bool_confirming=false;
            $this->bool_error=false;

            //check input
            if(!$this->bool_qcontrol_text_input_ok($this->textbox_bid))
            {
                //$this->append_line_confirming_text('bid input='.$this->textbox_bid->Text);
                //if(!$bool_error)$this->append_line_message('confirming');
                //$this->bool_error=true;
                $this->textbox_bid->Blink();
                $this->set_error_message('Please input your bid amount');
            }
            else
            {
                if(is_numeric($this->textbox_bid->Text))
                {
                    $bid_int_=intval($this->textbox_bid->Text);

                    if($bid_int_>=30 && $bid_int_<=100000)
                    {

                    }
                    else
                    {
                        $this->textbox_bid->Blink();
                        $this->set_error_message('Please input your bid amount within the range(30-10000)');
                    }
                }
                else
                {
                    $this->textbox_bid->Blink();
                    $this->set_error_message('Please input your bid amount as numeric');
                }
            }

            if(!$this->bool_qcontrol_text_input_ok($this->textbox_delivery_within))
            {
                //$this->append_line_confirming_text('textbox_delivery_within input='.$this->textbox_delivery_within->Text);
                //$this->bool_error=true;
                $this->textbox_delivery_within->Blink();
                $this->set_error_message('Please input your delivery day(s)');
            }
            else
            {
                if(is_numeric($this->textbox_delivery_within->Text))
                {
                    $bid_int_=intval($this->textbox_delivery_within->Text);

                    if($bid_int_>=1 && $bid_int_<=365)
                    {

                    }
                    else
                    {
                        $this->textbox_delivery_within->Blink();
                        $this->set_error_message('Please input your delivery day(s) within the range(1-365)');
                    }
                }
                else
                {
                    $this->textbox_bid->Blink();
                    $this->set_error_message('Please input your delivery day(s) as numeric');
                }
            }

            if($this->bool_qcontrol_text_input_ok($this->textbox_comment))
            {
                //$this->append_line_confirming_text('textbox_delivery_within input='.$this->textbox_delivery_within->Text);
                //$this->bool_error=true;
                
                if($this->bool_qcontrol_text_input_within($this->textbox_comment,200) && $this->bool_qcontrol_text_input_over($this->textbox_comment,6))
                {
                    
                }
                else
                {
                    $this->textbox_comment->Blink();
                    $this->set_error_message('Please input your textbox_comment within the range(6-365)');
                }
            }

            //$this->reset_message();
            //$this->append_line_message('confirming');
            //$this->bool_message_exist=true;

            if(!$this->bool_error)
            {
                $this->append_line_confirming_text('bid(*)='.$this->textbox_bid->Text);
                $this->append_line_confirming_text('delivery_within(*)='.$this->textbox_delivery_within->Text);
                $this->append_line_confirming_text('comment='.$this->textbox_comment->Text);

                $this->bool_confirming=true;
                $this->bool_message_exist=false;
            }
            else
            {
                $this->bool_message_exist=true;
            }
        }

        public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }

        public function back_Click($strFormId, $strControlId, $strParameter) {
            $this->bool_confirming=false;
            $this->bool_message_exist=false;
        }

        public function ok_Click($strFormId, $strControlId, $strParameter) {

            //save data
          	$objUser = unserialize($_SESSION['User']);
          	$this->UserId=$objUser->Id;
           
			
			$bid_= TableProjectBid::LoadById($this->bid);
            $bid_->Bid=intval($this->textbox_bid->Text);
            $bid_->DayToComplete=intval($this->textbox_delivery_within->Text);
            $bid_->Comment=$this->textbox_comment->Text;
            $bid_->set_bid_date_for_now();
            $bid_->set_status_active();
            $bid_->Enable='y';
            $bid_->BuyerId=$this->project->UserId;
            $bid_->UserId=$this->UserId;
            $bid_->ProjectId=$this->project->Id;
            $bid_->Save(false,true);

            QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');
        }
		


        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }

            if(is_numeric(QApplication::QueryString('bid')))
            {
                $this->bid_id=QApplication::QueryString('bid');//bid_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
			$objUser = unserialize($_SESSION['User']);
            $this->UserId=$objUser->Id;

                $this->project=TableProject::LoadById($this->id);
				$this->project_bidByUserID=TableProjectBid::CountByUserId($this->UserId);
								
                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] user_id=['. $this->project->UserId.']';
                }
                
           
                 else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
                if ($this->project_bidByUserID==0)
                	
                {
                	$this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.'] <br/> Bid is not placed<br/>';
                
                	  $this->id=null;
                    $this->project=null;
                }			
				
            }
        }
        protected function load_bid()
		{
		if(isset($this->id) && is_numeric($this->id) && isset($this->bid_id))
		{
			$this->bid=TableProjectBid::LoadById($this->bid_id);

			if(isset($this->bid))

			{
				$this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.'] Bid loaded-['.$this->bid->Id.']';

			}
			else
			{
				$this->contents_html='bid not found id=['.$this->bid_id.']';
				$this->id=null;
				$this->project=null;
				$this->bid_id=null;
			}
		}
	}

        //check permissions
        public function bool_project_loaded()
        {
            return isset($this->project);
        }

        public function bool_my_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        public function bool_won_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }

            return false;
        }

        protected function bool_already_bidding()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_bidding_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }
        
        public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }
        public function render_message_text() {

            if($this->bool_message_exist)
            {
                echo($this->str_message);
            }
        }
        protected function bool_already_won()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_won_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        public function bool_project_open()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }

        public function bool_project_after_picked()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }
        protected function bool_status()
	{
		if(isset($this->project)&& isset($this->UserId) && isset($this->bid))
		{
			return $this->bid->bool_active();
		}
		return false;

	}

        public function bool_project_bidding_end()
        {
            if(!bool_project_open() && !bool_project_after_picked())return true;

            return false;
        }
        
        protected function bool_bid_placed()
	{
		if(isset($this->project) && isset($this->UserId) && isset($this->bid))
		{

			if(TableProjectBid::bool_bid_id_placed_for_the_project($this->project->UserId,$this->project->Id,$this->bid))
			{
				return true;

			}
		}

		return false;
	}
        public function reset_permission_error_text() {

		$this->str_permission_error_text='';
	}
	 public function append_line_permission_error_text($str_) {

          $this->append_permission_error_text($str_.'<br />');
        }

     public function append_permission_error_text($str_) {

            $this->str_permission_error_text.=$str_;
        }
       

        protected function bool_edit_bid_permitted()
        {
        if(!$this->bool_logined()){

        	$this->reset_permission_error_text();
            $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
        if(!$this->bool_project_loaded()){

        	$this->reset_permission_error_text();
            $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
        if($this->bool_my_project()){

        	$this->reset_permission_error_text();
            $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
        if($this->bool_already_won()){

        	$this->reset_permission_error_text();
            $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
        if(!$this->bool_project_open()){
        	
			$this->reset_permission_error_text();
            $this->append_line_permission_error_text('<br/><br/> You must login to access this page at least');
			return false;
		}
		if(!$this->bool_bid_placed())       
		{
			  $this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You have not placed the bid');
			return false;
		}
		if(!$this->bool_status())       
		{
			$this->reset_permission_error_text();
                $this->append_line_permission_error_text('<br/><br/> You have cancelled the bid');
			return false;
		}
		
        if($this->bool_already_bidding())
            {
            	
                return true;
            }

            return true;
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }
		  public function bool_qcontrol_text_input_ok($qcontrol_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen(trim($qcontrol_->Text))>0)
                {
                    $qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }
		 public function append_line_confirming_text($str_) {

            $this->append_confirming_text($str_.'<br />');
        }
		 public function append_confirming_text($str_) {

            $this->str_confirming_text.=$str_;
        }
        
         public function render_confirming_text() {

            if($this->bool_confirming)
            {
                echo($this->str_confirming_text);
            }
        }

	}

    popup_edit_bid::Run('popup_edit_bid','popup_edit_bid.tpl.php');

?>